package cn.zld.sh.sh.ui.settingfragment;

import android.arch.lifecycle.ViewModelProviders;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import cn.zld.sh.sh.R;
import cn.zld.sh.sh.util.SharedUtils;

public class SettingFragment extends Fragment {

    private Switch switch_voice;
    private Switch switch_zhendong;
    private Switch switch_start;
    private SharedPreferences sp=null;

    private SettingFragmentViewModel mViewModel;
    private TextView appVersion;

    public static SettingFragment newInstance() {
        return new SettingFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.setting_fragment_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(SettingFragmentViewModel.class);

        sp = getActivity().getSharedPreferences("userinfo", getActivity().MODE_PRIVATE); //
        showVersion();
        initSwitch();
        // TODO: Use the ViewModel
    }
    
    private void showVersion(){
        appVersion = getActivity().findViewById(R.id.appVersion);

        try{
            PackageInfo info=getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(),0);//getPackageManager()获取设备安装应用程序包对象
            // getPackageInfo：根据包名获取此处flag标签
            appVersion.setText("V "+info.versionName);//程序版本信息
            appVersion.setTextSize(16);


        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
            appVersion.setText("V");
        }
    }

    private void initSwitch(){
        switch_voice = (Switch) getActivity().findViewById(R.id.switch_voice);
//        switch_zhendong = (Switch) getActivity().findViewById(R.id.switch_zhendong);
//        switch_start = (Switch) getActivity().findViewById(R.id.switch_start);

        switch_voice.setChecked(SharedUtils.getBoolean("switch_voice"));
//        switch_zhendong.setChecked(SharedUtils.getBoolean("switch_zhendong"));
//        switch_start.setChecked(SharedUtils.getBoolean("switch_start"));

//        Log.i("switch_voice", SharedUtils.getBoolean("switch_voice") + "");

        switch_voice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedUtils.putBoolean(getString(R.string.setting_switch_voice), isChecked);
            }
        });
//        Log.i("switch_voice", SharedUtils.getBoolean("switch_voice") + "");

//        switch_zhendong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                SharedUtils.putBoolean(getString(R.string.setting_switch_zhendong), isChecked);
//            }
//        });
//
//        switch_start.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                SharedUtils.putBoolean(getString(R.string.setting_switch_start), isChecked);
//            }
//        });
    }

}
